package com.sap.health.tracker.json;

import java.util.ArrayList;
import java.util.List;
 
import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;
import org.json.JSONException;
import org.json.JSONObject;


 
import android.content.Context;
 
public class JSONFunctions {
 
    private JSONParser jsonParser;
 
    // Testing in localhost using wamp or xampp
    // use http://10.0.2.2/ to connect to your localhost ie http://localhost/
   /* private static String loginURL = "http://27.33.44.229/CSSE3004/website/android_function/";
    private static String registerURL = "http://27.33.44.229/CSSE3004/website/android_function/";
    private static String checkLoginURL = "http://27.33.44.229/CSSE3004/website/android_function/";
    private static String sessionDestroyURL = "http://27.33.44.229/CSSE3004/website/android_function/";
  */  
    private static String localhost = "http://10.0.2.2/CSSE3004/website/android_function/";
    
    private static String loginURL = localhost;
    private static String registerURL = localhost;
    private static String checkLoginURL = localhost;
    private static String sessionDestroyURL = localhost;

    private static String login_tag = "login";
    private static String register_tag = "register";
 
    // constructor
    public JSONFunctions(){
        jsonParser = new JSONParser();
    }
 
    
    /**
     * function make Login Request
     * @param email
     * @param password
     * */
    public JSONObject checkLoginUser(String userID, String sessionToken){
        // Building Parameters
        List<NameValuePair> params = new ArrayList<NameValuePair>();
        params.add(new BasicNameValuePair("tag", "check_tag"));
        params.add(new BasicNameValuePair("userID", userID));
        params.add(new BasicNameValuePair("sessionToken", sessionToken));
        JSONObject json = jsonParser.getJSONFromUrl(checkLoginURL, params);
        // return json
        // Log.e("JSON", json.toString());
        return json;
    }
    
    
    /**
     * function make Login Request
     * @param email
     * @param password
     * */
    public JSONObject loginUser(String username, String password){
        // Building Parameters
        List<NameValuePair> params = new ArrayList<NameValuePair>();
        params.add(new BasicNameValuePair("tag", login_tag));
        params.add(new BasicNameValuePair("username", username));
        params.add(new BasicNameValuePair("password", password));
        JSONObject json = jsonParser.getJSONFromUrl(loginURL, params);
        // return json
        // Log.e("JSON", json.toString());
        return json;
    }
 
    /**
     * function make Login Request
     * @param name
     * @param email
     * @param password
     * */
    public JSONObject registerUser(String username, String email, 
    		String password, String firstName, String lastName, String address,
    		String contact, String dob, String gender, String role){
        // Building Parameters
        List<NameValuePair> params = new ArrayList<NameValuePair>();
        params.add(new BasicNameValuePair("tag", register_tag));
        params.add(new BasicNameValuePair("username", username));
        params.add(new BasicNameValuePair("email", email));
        params.add(new BasicNameValuePair("password", password));
        params.add(new BasicNameValuePair("firstName", firstName));
        params.add(new BasicNameValuePair("lastName", lastName));
        params.add(new BasicNameValuePair("address", address));
        params.add(new BasicNameValuePair("contact", contact));
        params.add(new BasicNameValuePair("dob", dob));
        params.add(new BasicNameValuePair("gender", gender));
        params.add(new BasicNameValuePair("role", role));
 
        // getting JSON Object
        JSONObject json = jsonParser.getJSONFromUrl(registerURL, params);
        // return json
        return json;
    }
    
    /**
     * function make Login Request
     * @param name
     * @param email
     * @param password
     * */
    public JSONObject getDoctorProfile(String userID, String session){
        // Building Parameters
        List<NameValuePair> params = new ArrayList<NameValuePair>();
        params.add(new BasicNameValuePair("tag", "get_profile"));
        params.add(new BasicNameValuePair("userID", userID));
        params.add(new BasicNameValuePair("session", session));
 
        // getting JSON Object
        JSONObject json = jsonParser.getJSONFromUrl(registerURL, params);
        // return json
        return json;
    }
    
    /**
     * function make Login Request
     * @param name
     * @param email
     * @param password
     * */
    public JSONObject getNotifications(String userID, String session){
        // Building Parameters
        List<NameValuePair> params = new ArrayList<NameValuePair>();
        params.add(new BasicNameValuePair("tag", "get_notifications"));
        params.add(new BasicNameValuePair("userID", userID));
        params.add(new BasicNameValuePair("session", session));
 
        // getting JSON Object
        JSONObject json = jsonParser.getJSONFromUrl(registerURL, params);
        // return json
        return json;
    }
    
    /**
     * function make Login Request
     * @param name
     * @param email
     * @param password
     * */
    public JSONObject getHospitals(String userID, String session){
        // Building Parameters
        List<NameValuePair> params = new ArrayList<NameValuePair>();
        params.add(new BasicNameValuePair("tag", "get_hospitals"));
        params.add(new BasicNameValuePair("userID", userID));
        params.add(new BasicNameValuePair("session", session));
 
        // getting JSON Object
        JSONObject json = jsonParser.getJSONFromUrl(registerURL, params);
        // return json
        return json;
    }
    
    /**
     * function make Login Request
     * @param name
     * @param email
     * @param password
     * */
    public JSONObject saveGender(String userID, String session, 
    		String gender){
        // Building Parameters
        List<NameValuePair> params = new ArrayList<NameValuePair>();
        params.add(new BasicNameValuePair("tag", "save_gender"));
        params.add(new BasicNameValuePair("userID", userID));
        params.add(new BasicNameValuePair("session", session));
        params.add(new BasicNameValuePair("gender", gender));
        JSONObject json = null;
        // getting JSON Object
		json = jsonParser.getJSONFromUrl(registerURL, params);
		// return json
        return json;
    }
    
    /**
     * function make Login Request
     * @param name
     * @param email
     * @param password
     * */
    public JSONObject pushNotifications(String userID, String session, String regId){
        // Building Parameters
        List<NameValuePair> params = new ArrayList<NameValuePair>();
        params.add(new BasicNameValuePair("tag", "register_push_notifications"));
        params.add(new BasicNameValuePair("userID", userID));
        params.add(new BasicNameValuePair("session", session));
        params.add(new BasicNameValuePair("regId", regId));


        // getting JSON Object
        JSONObject json = jsonParser.getJSONFromUrl(registerURL, params);
        // return json
        return json;
    }
    
    /**
     * function make Login Request
     * @param name
     * @param email
     * @param password
     * */
    public JSONObject saveDOB(String userID, String session, String year, 
    		String month, String day){
        // Building Parameters
        List<NameValuePair> params = new ArrayList<NameValuePair>();
        params.add(new BasicNameValuePair("tag", "save_dob"));
        params.add(new BasicNameValuePair("userID", userID));
        params.add(new BasicNameValuePair("session", session));
        params.add(new BasicNameValuePair("year", year));
        params.add(new BasicNameValuePair("month", month));
        params.add(new BasicNameValuePair("day", day));

 
        // getting JSON Object
        JSONObject json = jsonParser.getJSONFromUrl(registerURL, params);
        // return json
        return json;
    }
    
    /**
     * function make Login Request
     * @param name
     * @param email
     * @param password
     * */
    public JSONObject saveName(String userID, String session, 
    		String firstname, String lastname){
        // Building Parameters
        List<NameValuePair> params = new ArrayList<NameValuePair>();
        params.add(new BasicNameValuePair("tag", "save_name"));
        params.add(new BasicNameValuePair("userID", userID));
        params.add(new BasicNameValuePair("session", session));
        params.add(new BasicNameValuePair("firstname", firstname));
        params.add(new BasicNameValuePair("lastname", lastname));
 
        // getting JSON Object
        JSONObject json = jsonParser.getJSONFromUrl(registerURL, params);
        // return json
        return json;
    }
    
    /**
     * function make Login Request
     * @param name
     * @param email
     * @param password
     * */
    public JSONObject getConnectionList(String userID, String session){
        // Building Parameters
        List<NameValuePair> params = new ArrayList<NameValuePair>();
        params.add(new BasicNameValuePair("tag", "get_connection_list"));
        params.add(new BasicNameValuePair("userID", userID));
        params.add(new BasicNameValuePair("session", session));
 
        // getting JSON Object
        JSONObject json = jsonParser.getJSONFromUrl(registerURL, params);
        // return json
        return json;
    }
    
    /**
     * function make Login Request
     * @param name
     * @param email
     * @param password
     * */
    public JSONObject acceptPatient(String userID, String session, String patient){
        // Building Parameters
        List<NameValuePair> params = new ArrayList<NameValuePair>();
        params.add(new BasicNameValuePair("tag", "accept_patient_request"));
        params.add(new BasicNameValuePair("userID", userID));
        params.add(new BasicNameValuePair("session", session));
        params.add(new BasicNameValuePair("patient", patient));

 
        // getting JSON Object
        JSONObject json = jsonParser.getJSONFromUrl(registerURL, params);
        // return json
        return json;
    }
    
    /**
     * function make Login Request
     * @param name
     * @param email
     * @param password
     * */
    public JSONObject removePatientFrom(String userID, String session, String patient){
        // Building Parameters
        List<NameValuePair> params = new ArrayList<NameValuePair>();
        params.add(new BasicNameValuePair("tag", "remove_patient_request"));
        params.add(new BasicNameValuePair("userID", userID));
        params.add(new BasicNameValuePair("session", session));
        params.add(new BasicNameValuePair("patient", patient));

 
        // getting JSON Object
        JSONObject json = jsonParser.getJSONFromUrl(registerURL, params);
        // return json
        return json;
    }
 
    /**
     * function make Login Request
     * @param email
     * @param password
     * */
    public JSONObject logOutUser(String userID, String sessionToken){
        // Building Parameters
        List<NameValuePair> params = new ArrayList<NameValuePair>();
        params.add(new BasicNameValuePair("tag", "delete_session_tag"));
        params.add(new BasicNameValuePair("userID", userID));
        params.add(new BasicNameValuePair("sessionToken", sessionToken));
        JSONObject json = jsonParser.getJSONFromUrl(sessionDestroyURL, params);
        // return json
        // Log.e("JSON", json.toString());
        return json;
    }
 
}